package com.alan.webscoket;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

/**
 * Created by Alan on 2017/8/6.
 */
@ServerEndpoint("/echo")
public class EchoSocket {

    public EchoSocket(){
        System.out.println("EchoSocket.EchoSocket()");
    }

    @OnOpen
    public void open(Session session){
        //一个session 代表一个通信会话
        System.out.println("sessionid="+session.getId());


    }

    @OnMessage
    public void message(Session session,String msg){

        System.out.println("客户端：="+msg);
        try {
            session.getBasicRemote().sendText("服务端：ni hao too");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @OnClose
    public void close(Session session){
        System.out.println("session:"+session.getId()+"关闭!");

    }


}
